package com.zero.logistics.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zero.logistics.pojo.MessageObject;
import com.zero.logistics.pojo.Permission;
import com.zero.logistics.pojo.PermissionExample;
import com.zero.logistics.pojo.PermissionExample.Criteria;
import com.zero.logistics.service.PermissionService;

/*
 *	@date:2020年3月31日 上午5:00:13
 *	@author:Zero
 *	@version:Future
 *	@description: 
 *
 */
@Controller
@RequestMapping("/permission")
public class PermissionController {
	
	@Autowired
	private PermissionService permissionService;

	@RequiresPermissions("permission:permissionPage")
	@RequestMapping("/permissionPage")
	public String permissionPage() {
		return "permissionPage";
	}
	
	/**
	 * @function 分页显示权限
	 * @param pageNum
	 * @param pageSize
	 * @param keywords
	 * @return
	 */
	@RequiresPermissions("permission:list")
	@RequestMapping("list")
	@ResponseBody  //返回json类型的数据
	public PageInfo<Permission> permissionList(@RequestParam(defaultValue = "1")int pageNum,
			@RequestParam(defaultValue = "10")int pageSize,
			String keywords){
		
		PageHelper.startPage(pageNum, pageSize);
		PermissionExample example = new PermissionExample();
		
		// 定义搜索的逻辑（使用管理）
		if (StringUtils.isNotBlank(keywords)) {  //当前台传过来的关键字不等于空的时候
			//根据权限名查询
			Criteria criteria1 = example.createCriteria();
			criteria1.andNameLike("%" + keywords + "%");
			
			//根据父权限名查询
			Criteria criteria2 = example.createCriteria();
			criteria2.andParentNameLike("%" + keywords + "%");
			example.or(criteria2);
		}
		
		List<Permission> permissions = permissionService.selectByExample(example);
		PageInfo<Permission> pageInfo = new PageInfo<>(permissions);
		
		return pageInfo;
	}
	
	@RequestMapping("/getAllPermission")
	@ResponseBody
	public List<Permission> getAllPermission(){
		PermissionExample example = new PermissionExample();
		List<Permission> permissions = permissionService.selectByExample(example );
		return permissions;
	}
	
	/**
	 * @function 删除权限
	 * @param permissionId
	 * @return
	 */
	@RequiresPermissions("permission:delete")
	@RequestMapping("/delete")
	@ResponseBody
	public MessageObject permissionDelete(Long permissionId) {
		
		//1.删除权限之前要判断当前权限是否还拥有子权限，如果拥有子权限就不能删除
		PermissionExample example = new PermissionExample();
		Criteria criteria = example.createCriteria();
		criteria.andParentIdEqualTo(permissionId);
		List<Permission> childPermissions = permissionService.selectByExample(example);
		if (childPermissions.size() > 0) {
			return MessageObject.createMo(0, "该权限下还有子权限，不能删除");
		}
		//2.根据权限id删除权限
		int row = permissionService.deleteByPrimaryKey(permissionId);
		MessageObject mo = MessageObject.createMo(0, "删除数据失败，请联系技术人员");
		if (row == 1) {
			mo = MessageObject.createMo(1, "删除数据成功");
		}
		return mo;
	}
	
	/**
	 * @function 批量删除(以String类型接收前端传过来的参数)
	 * @param map
	 * @return
	 */
	@RequiresPermissions("permission:batchdel")
	@RequestMapping("/batchdel")
	@ResponseBody
	public MessageObject permissionBatchdel(String permissionIds) {
		System.out.println(permissionIds);
		String[] permissionIdsArr = permissionIds.split(",");
		
		//1.删除权限之前要判断当前所有的权限是否还拥有子权限，如果拥有子权限就不能删除
		for (String permissionId : permissionIdsArr) {
			PermissionExample example = new PermissionExample();
			Criteria criteria = example.createCriteria();
			criteria.andParentIdEqualTo(Long.parseLong(permissionId));
			List<Permission> childPermissions = permissionService.selectByExample(example);
			if (childPermissions.size() > 0) {
				return MessageObject.createMo(0, "将要删除的权限下还有子权限，不能删除");
			}
		}
		//2.根据权限id的数组批量删除权限
		int rows = permissionService.deleteByIds(permissionIdsArr);
		MessageObject mo = MessageObject.createMo(0, "批量删除数据失败，请联系技术人员");
		if (rows != 0) {
			mo = MessageObject.createMo(1, "批量删除数据成功");
		}
		return mo;
	}
	
	/**
	 * @function 提供编辑页面(在新增权限和更新权限的时候都需要执行此方法)
	 * @param m
	 * @return
	 */
	@RequestMapping("/edit")
	public String permissionEdit(Model m, Long permissionId) {	
		//1.当前端传过来的permissionId不为空的时候，就通过permissionId查询权限信息并共享数据，用于更新的情况
		if (permissionId != null) {
			Permission permission = permissionService.selectByPrimaryKey(permissionId);
			System.out.println("需要更新的权限信息：" + permission.getPermissionId());
			m.addAttribute("permission", permission);
		}
		//2.获取所有的父权限信息并共享（查询出所有type=menu的权限作为父权限）
		PermissionExample example = new PermissionExample();
		Criteria criteria = example.createCriteria();
		criteria.andTypeEqualTo("menu");
		List<Permission> parentPermissions = permissionService.selectByExample(example);
		m.addAttribute("parentPermissions", parentPermissions);
		return "permissionEdit";
	}
	
	/**
	 * @function 判断权限名是否存在
	 * @param name
	 * @return
	 */
	@ResponseBody
	@RequiresPermissions("permission:checkName")
	@RequestMapping("/checkName")
	public boolean permissionCheckName(String name) {
		PermissionExample example = new PermissionExample();
		Criteria criteria = example.createCriteria();
		criteria.andNameEqualTo(name);
		List<Permission> permissions = permissionService.selectByExample(example);
		return permissions.size() == 0 ? true : false;
	}
	
	/**
	 * @function 新增权限
	 * @param permission
	 * @return
	 */
	@RequiresPermissions("permission:insert")
	@RequestMapping("/insert")
	@ResponseBody
	public MessageObject permissionInsert(Permission permission) {
		System.out.println("hello");
		// 1.插入数据并返回结果
		int row = permissionService.insert(permission);
		MessageObject mo = MessageObject.createMo(0, "添加数据失败，请联系技术人员");
		if (row == 1) {
			mo = MessageObject.createMo(1, "添加数据成功");
		}
		return mo;
	}
	
	@RequiresPermissions("permission:update")
	@RequestMapping("update")
	@ResponseBody
	public MessageObject permissionUpdate(Permission permission) {
		// 1.更新数据并返回结果(这里要用动态sql语句)
		int row = permissionService.updateByPrimaryKeySelective(permission);
		MessageObject mo = MessageObject.createMo(0, "更新数据失败，请联系技术人员");
		if (row == 1) {
			mo = MessageObject.createMo(1, "更新数据成功");
		}
		return mo;
	}

}





